package cn.iocoder.lw.module.core.controller.app.project;

import cn.iocoder.lw.framework.common.pojo.CommonResult;
import cn.iocoder.lw.framework.common.pojo.PageResult;
import cn.iocoder.lw.framework.common.util.object.BeanUtils;
import cn.iocoder.lw.framework.ip.core.utils.AreaUtils;
import cn.iocoder.lw.module.core.controller.admin.project.vo.CoreProjectPageReqVO;
import cn.iocoder.lw.module.core.controller.admin.project.vo.CoreProjectRespVO;
import cn.iocoder.lw.module.core.controller.admin.project.vo.CoreProjectSaveReqVO;
import cn.iocoder.lw.module.core.service.project.CoreProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static cn.iocoder.lw.framework.common.pojo.CommonResult.success;
import static cn.iocoder.lw.framework.web.core.util.WebFrameworkUtils.getLoginUserId;

@Tag(name = "用户 APP - 核心项目")
@RestController
@RequestMapping("/core/project")
@Validated
@Slf4j
public class AppCoreProjectController {

    @Resource
    private CoreProjectService projectService;

    @PostMapping("/create")
    @Operation(summary = "创建项目")
    @PreAuthorize("@ss.hasPermission('core:project:create')")
    public CommonResult<Long> createProject(@Valid @RequestBody CoreProjectSaveReqVO createReqVO) {
        // 转换为管理后台的VO
        CoreProjectSaveReqVO adminSaveReqVO = BeanUtils.toBean(createReqVO, CoreProjectSaveReqVO.class);
        // 设置负责人为当前用户
        adminSaveReqVO.setOwnerUserId(getLoginUserId());

        return success(projectService.createProject(adminSaveReqVO, getLoginUserId()));
    }

    @PutMapping("/update")
    @Operation(summary = "更新项目")
    @PreAuthorize("@ss.hasPermission('core:project:update')")
    public CommonResult<Boolean> updateProject(@Valid @RequestBody CoreProjectSaveReqVO updateReqVO) {
        // 转换为管理后台的VO
        CoreProjectSaveReqVO adminSaveReqVO = BeanUtils.toBean(updateReqVO, CoreProjectSaveReqVO.class);

        projectService.updateProject(adminSaveReqVO);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得项目")
    @Parameters({
            @Parameter(name = "id", description = "编号", required = true, example = "1024")
    })
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<CoreProjectSaveReqVO> getProject(@RequestParam("id") Long id) {
        CoreProjectRespVO project = projectService.getProject(id);
        if (project == null) {
            return success(null);
        }
        // 转换为APP的VO
        CoreProjectSaveReqVO appProject = BeanUtils.toBean(project, CoreProjectSaveReqVO.class);

        return success(appProject);
    }

    @GetMapping("/page")
    @Operation(summary = "获得项目分页")
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<PageResult<CoreProjectSaveReqVO>> getProjectPage(@Valid CoreProjectSaveReqVO pageReqVO) {
        Long currentUserId = getLoginUserId();


        // 转换为管理后台的VO
        CoreProjectPageReqVO adminPageReqVO = BeanUtils.toBean(pageReqVO, CoreProjectPageReqVO.class);

        PageResult<CoreProjectRespVO> pageResult = projectService.getProjectPage(adminPageReqVO);
        PageResult<CoreProjectSaveReqVO> appPageResult = BeanUtils.toBean(pageResult, CoreProjectSaveReqVO.class);
        return success(appPageResult);
    }

    @GetMapping("/my-page")
    @Operation(summary = "获得我负责的项目分页")
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<PageResult<CoreProjectSaveReqVO>> getMyProjectPage(@Valid CoreProjectSaveReqVO pageReqVO) {
        // 强制只查询当前用户负责的项目
        pageReqVO.setOwnerUserId(getLoginUserId());

        // 转换为管理后台的VO
        CoreProjectPageReqVO adminPageReqVO = BeanUtils.toBean(pageReqVO, CoreProjectPageReqVO.class);

        PageResult<CoreProjectRespVO> pageResult = projectService.getProjectPage(adminPageReqVO);
        PageResult<CoreProjectSaveReqVO> appPageResult = BeanUtils.toBean(pageResult, CoreProjectSaveReqVO.class);

        return success(appPageResult);
    }

    @GetMapping("/my-list")
    @Operation(summary = "获得我负责的项目列表")
    @PreAuthorize("@ss.hasPermission('core:project:query')")
    public CommonResult<List<CoreProjectSaveReqVO>> getMyProjectList() {
        Long currentUserId = getLoginUserId();
        List<CoreProjectRespVO> list = projectService.getProjectListByOwnerUserId(currentUserId);

        // 转换为APP的VO
        List<CoreProjectSaveReqVO> appList = BeanUtils.toBean(list, CoreProjectSaveReqVO.class);

        return success(appList);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除项目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('core:project:delete')")
    public CommonResult<Boolean> deleteProject(@RequestParam("id") Long id) {
        projectService.deleteProject(id);
        return success(true);
    }

}